<?php

class step2Action extends sfAction
{
    public function execute($request)
    {
        $this->shop_profile_form = new ShopProfileForm();
        unset($this->shop_profile_form[$this->shop_profile_form->getCSRFFieldName()]);
        $page = $this->getContext()->getUser()->getPage();
        $this->is_wall_post_active = $page->WallConfig->is_active;
        
        if ($request->getMethod() === sfRequest::POST && $request->hasParameter($this->shop_profile_form->getName())) {
            $this->processForm($request, $this->shop_profile_form);
            $this->url_params = LogInstall::log($request, 'step2_form_error');
        } else {
            $this->url_params = LogInstall::log($request, 'step2');
        }

        return sfView::SUCCESS;
    }


    private function processForm(sfWebRequest $request, ShopProfileForm $form)
    {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            // Here we save Shop profile and go to step 3
            $form_data = $request->getParameter('shop_profile');
            $shop_profile = new ShopProfile();
            $shop_profile->page_id = $this->getContext()->getUser()->getPage()->page_id;
            $shop_profile->name = $form_data['name'];
            $shop_profile->description = $form_data['description'];
            $shop_profile->keyword = $form_data['keyword'];
            $shop_profile->email = $form_data['email'];
            $shop_profile->created_at = date(DATE_ISO8601);
            $shop_profile->updated_at = date(DATE_ISO8601);
            $shop_profile->save();
            $this->redirect('@install_step3');
        }
    }
}

